package com.woniu.ownermanage.controller;

import com.woniu.common.ResponseData;
import com.woniu.common.ResponseEnum;
import com.woniu.ownermanage.entity.ParkingPlace;
import com.woniu.ownermanage.entity.ParkingRecord;
import com.woniu.ownermanage.entity.ScmspOwnerCar;
import com.woniu.ownermanage.entity.ScmspOwnerCarOrder;
import com.woniu.ownermanage.service.ParkingPlaceService;
import com.woniu.ownermanage.service.ParkingRecordService;
import com.woniu.ownermanage.service.ScmspOwnerCarOrderService;
import com.woniu.ownermanage.service.ScmspOwnerCarService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@RestController
@RequestMapping("/parkingService")
public class ParkingController {
    @Autowired
    private ParkingRecordService parkingRecordService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ParkingPlaceService parkingPlaceService;
    @Autowired
    private ScmspOwnerCarService scmspOwnerCarService;
    @Autowired
    private ScmspOwnerCarOrderService scmspOwnerCarOrderService;

    @PostMapping("/addCarParkingRecord")
    @ApiOperation(value = "增加或更新停车记录",notes = "根据图片获取车牌号")
    public ResponseData<?> getCarId(@RequestBody String image){
        return parkingRecordService.addCarParkingRecord(image);
    }

    @PostMapping("/queryAllRecord")
    public ResponseData<?> queryAllRecord(){
        return parkingRecordService.getAllRecord();
    }
    /**
     * 查询空车位
     */
    @GetMapping("/queryKong")
    public ResponseData<?> queryKong(){
        Long count = parkingPlaceService.lambdaQuery().eq(ParkingPlace::getStatus,"0").count();
        return new ResponseData<>().ok(count);
    }
    @PostMapping("/queryCarId")
    public ResponseData<?> queryCarId(@RequestParam("carId") String carId){
        ParkingRecord parkingRecord = parkingRecordService.lambdaQuery().eq(ParkingRecord::getCarId, carId).eq(ParkingRecord::getStatus, 0).one();
        if (parkingRecord!=null) {
            Date startTime = parkingRecord.getStartTime();
            Date endTime = parkingRecord.getEndTime();
            LocalDateTime startDateTime = startTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            LocalDateTime endDateTime = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            Duration parkingTime = Duration.between(startDateTime, endDateTime);
            long minutes = parkingTime.toMinutes();

            ScmspOwnerCarOrder carOrder = scmspOwnerCarOrderService.lambdaQuery().eq(ScmspOwnerCarOrder::getCarId, carId).eq(ScmspOwnerCarOrder::getStatus, 0).one();
            // 计算15分钟的块数
            long money = minutes / 15;
            int money1 = (int) money;
            carOrder.setMoney(money1);
            scmspOwnerCarOrderService.updateById(carOrder);
            return new ResponseData<>().ok(minutes);
        }else {
            return new ResponseData<>().fail(ResponseEnum.NULL_ERROR);
        }
    }
    @PostMapping("/queryById")
    public ResponseData<?> queryById(@RequestParam("carId") String carId){
        ScmspOwnerCarOrder carOrder = scmspOwnerCarOrderService.lambdaQuery().eq(ScmspOwnerCarOrder::getCarId, carId).eq(ScmspOwnerCarOrder::getStatus, 0).one();
        if (carOrder==null){
            return new ResponseData<>().fail(ResponseEnum.NULL_ERROR);
        }
        return new ResponseData<>().ok(carOrder.getId().toString());
    }
}
